/*
 * @# UserRedis.java 2011-12-24 下午02:28:38
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.user.redis;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.ianswer.common.util.time.SecondsUtils;
import com.ianswer.px.common.redis.RedisSupport;
import com.ianswer.px.user.entity.UserBaseInfo;

/**
 * 用户资源缓存.
 * 
 * @author alvin hwang
 */
public class UserRedis extends RedisSupport {

	private static final String USER_PREFIX = "user:";
	private static final String EMAIL = "email";
	private static final String NICKNAME = "nickname";
	private static final String TYPE = "type";
	private static final String AVATAR = "avatar";
	private static final String DOMAIN = "domain";
	private static final String PROFESSION = "profession";
	private static final String PROVINCENAME = "provincename";
	private static final String CITYNAME = "cityname";
	private static final String LOGINTIME = "logintime";

	public static void init(UserBaseInfo userBaseInfo) {
		Map<String, String> userCache = new HashMap<String, String>();

		userCache.put(EMAIL, userBaseInfo.getEmail());
		userCache.put(NICKNAME, userBaseInfo.getNickname());
		userCache.put(TYPE, userBaseInfo.getUserType().toString());
		userCache.put(AVATAR, StringUtils.defaultString(userBaseInfo.getAvatar()));
		userCache.put(DOMAIN, StringUtils.defaultString(userBaseInfo.getDomain()));
		userCache.put(PROFESSION, StringUtils.defaultString(userBaseInfo.getProfession()));
		userCache.put(LOGINTIME, SecondsUtils.millis().toString());
		userCache.put(PROVINCENAME, StringUtils.defaultString(userBaseInfo.getProvinceName()));
		userCache.put(CITYNAME, StringUtils.defaultString(userBaseInfo.getCityName()));
		redis().hmset(key(userBaseInfo.getId()), userCache);
	}

	public static void setAvatar(String avatar, Long userId) {
		Map<String, String> userCache = redis().hgetAll(key(userId));
		userCache.put(AVATAR, avatar);
	}

	public static void setArea(String proivceName, String cityName, Long userId) {
		Map<String, String> userCache = redis().hgetAll(key(userId));
		userCache.put(PROVINCENAME, proivceName);
		userCache.put(CITYNAME, cityName);
	}

	public static UserBaseInfo get(Long userId) {
		Map<String, String> userCache = redis().hgetAll(key(userId));
		if (MapUtils.isEmpty(userCache)) {
			return null;
		}
		UserBaseInfo user = new UserBaseInfo();
		user.setId(userId);
		user.setEmail(userCache.get(EMAIL));
		user.setNickname(userCache.get(NICKNAME));
		user.setUserType(Long.valueOf(userCache.get(TYPE)));
		user.setAvatar(userCache.get(AVATAR));
		user.setDomain(userCache.get(DOMAIN));
		user.setProfession(userCache.get(PROFESSION));
		user.setProvinceName(userCache.get(PROVINCENAME));
		user.setCityName(userCache.get(CITYNAME));
		return user;
	}

	public static void logintime(Long userId, Long time) {
		redis().hset(key(userId), LOGINTIME, String.valueOf(time));
	}

	private static String key(Long userId) {
		return USER_PREFIX + userId;
	}
}
